package com.sneakxy.mybase4j.platform.provider;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.sneakxy.mybase4j.base.provider.BaseProvider;
import com.sneakxy.mybase4j.platform.beans.Permission;
import com.sneakxy.mybase4j.platform.repository.PermissionRepository;

/**
 * 
 * @author 潜行的青衣
 * @see
 */
@Service(version = "1.0.0", interfaceClass = IPermissionProvider.class)
@org.springframework.stereotype.Service
public class PermissionProvider extends BaseProvider<Permission, String, PermissionRepository> implements IPermissionProvider {

	@Autowired
	public PermissionProvider(PermissionRepository repository) {
		super(repository);
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.platform.provider.IPermissionProvider#findAllByParentId(boolean)
	 */
	@Override
	public List<Permission> findAllByParentId(boolean hasChildren) {
		List<Permission> result = this.getRepository().findAllByParentIdIsNullOrderBySort();
		if(hasChildren) {
			Iterator<Permission> it = result.iterator();
			Permission perm = null;
			while(it.hasNext()) {
				perm = it.next();
				perm.setChildren(this.findAllByParentId(perm.getId(), hasChildren));
			}
		}
		return result;
	}

	public List<Permission> findAllByParentId(String parent_id, boolean hasChildren) {
		List<Permission> result = this.getRepository().findAllByParentIdOrderBySort(parent_id);
		if(hasChildren) {
			Iterator<Permission> it = result.iterator();
			Permission perm = null;
			while(it.hasNext()) {
				perm = it.next();
				perm.setChildren(this.findAllByParentId(perm.getId(), hasChildren));
			}
		}
		return result;
	}
	
}
